package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.quick.ar.server.base.common.enums.MaterialTypeEnum;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.material.vo.request.AdminUpdateMaterialRequest;
import com.gxar.quick.ar.server.base.model.material.vo.request.AdminUploadMaterialRequest;
import com.gxar.quick.ar.server.base.model.material.vo.request.QueryMaterialListRequest;
import com.gxar.quick.ar.server.base.model.material.vo.response.*;
import com.gxar.quick.ar.server.db.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/1/22 10:12
 */
@Api(tags = "管理端素材接口")
@RestController
@RequestMapping("/admin/api/materials")
@RequiredArgsConstructor
@Slf4j
public class AdminMaterialController {

    private final MaterialService materialService;

    @ApiOperation("上传素材")
    @PostMapping("")
    public Result<AdminUploadMaterialResponse> uploadMaterial(@RequestBody AdminUploadMaterialRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        Integer type = request.getType();
        MaterialTypeEnum materialTypeEnum = MaterialTypeEnum.getByCode(type);
        if (Objects.isNull(materialTypeEnum)) {
            throw new APIRuntimeException(ResultCodeEnum.MATERIAL_TYPE_ERROR);
        }
        if (MaterialTypeEnum.VIDEO_BOX.equals(materialTypeEnum)) {
            Optional.ofNullable(request.getInnerHeight()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_HEIGHT_NOT_NULL));
            Optional.ofNullable(request.getInnerWidth()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_WIDTH_NOT_NULL));
            Optional.ofNullable(request.getInnerX()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_X_NOT_NULL));
            Optional.ofNullable(request.getInnerY()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_Y_NOT_NULL));
        }
        AdminUploadMaterialResponse response = materialService.uploadMaterial(userProfile, request);
        return Result.success(response);
    }

    @ApiOperation("更新素材")
    @PutMapping("{id}")
    public Result<AdminUpdateMaterialResponse> updateMaterial(@PathVariable("id") Long id, @RequestBody AdminUpdateMaterialRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        Integer type = request.getType();
        MaterialTypeEnum materialTypeEnum = MaterialTypeEnum.getByCode(type);
        if (Objects.isNull(materialTypeEnum)) {
            throw new APIRuntimeException(ResultCodeEnum.MATERIAL_TYPE_ERROR);
        }
        if (MaterialTypeEnum.VIDEO_BOX.equals(materialTypeEnum)) {
            Optional.ofNullable(request.getInnerHeight()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_HEIGHT_NOT_NULL));
            Optional.ofNullable(request.getInnerWidth()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_WIDTH_NOT_NULL));
            Optional.ofNullable(request.getInnerX()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_X_NOT_NULL));
            Optional.ofNullable(request.getInnerY()).orElseThrow(() -> new APIRuntimeException(ResultCodeEnum.MATERIAL_INNER_Y_NOT_NULL));
        }
        AdminUpdateMaterialResponse response = materialService.updateMaterial(userProfile, id, request);
        return Result.success(response);
    }

    @ApiOperation("分页查询素材列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "number"),
    })
    @GetMapping("")
    public Result<PageResult<AdminMaterialPageListResponse>> pageList(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                                  @RequestParam(name = "page_size", defaultValue = "10") int pageSize,
                                                                  @RequestParam(name =  "type") int type) {
        return materialService.pageList(pageNum, pageSize, type);
    }

    @ApiOperation("素材详情")
    @GetMapping("/{id}")
    public Result<AdminMaterialDetailResponse> materialDetail(@PathVariable("id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        AdminMaterialDetailResponse response = materialService.materialDetail(userProfile, id);
        return Result.success(response);
    }

    @ApiOperation("删除素材")
    @DeleteMapping("/{id}")
    public Result<AdminDeletedMaterialResponse> deleteMaterial(@PathVariable("id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        materialService.deleteMaterial(userProfile, id);
        return Result.success();
    }
}
